package cn.huiyunche.base.service.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description: (功能详细描述)
 */
public class StringValidate {

    public static boolean isEmail(String content) {
        // 邮箱格式正则表达式
        String mailRegex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        if (StringUtils.isEmpty(content))
            return false;
        Pattern regex = Pattern.compile(mailRegex);
        Matcher matcher = regex.matcher(content);
        return matcher.matches();
    }

    public static boolean isChEnBracket(String content) {
        String mailRegex = "^(([a-zA-Z0-9\u4E00-\u9FA5()（）])+)$";
        if (StringUtils.isEmpty(content))
            return false;
        Pattern regex = Pattern.compile(mailRegex);
        Matcher matcher = regex.matcher(content);
        return matcher.matches();
    }

    public static boolean isMobile(String content) {
        String mobileRegex = "^(1[0-9][0-9]\\d{8})$";
        if (StringUtils.isEmpty(content))
            return false;
        Pattern regex = Pattern.compile(mobileRegex);
        Matcher matcher = regex.matcher(content);
        return matcher.matches();
    }

    public static boolean isChinese(String content) {
        if (StringUtils.isEmpty(content))
            return false;
        char[] chars = content.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (!isChineseChar(chars[i])) {
                return false;
            }
        }

        return true;
    }

    /**
     * 字符是否为中文
     *
     * @param cha
     * @return
     * @author: 赵贵欣 Date : 2016年1月14日 上午9:34:04
     */
    private static boolean isChineseChar(char cha) {
        String regexStr = "[\u4E00-\u9FA5]";
        String content = String.valueOf(cha);
        if (StringUtils.isEmpty(content))
            return false;
        return Pattern.matches(regexStr, content);
    }

    public static long length(String content) {
        if (StringUtils.isEmpty(content))
            return 0;
        char[] chars = content.toCharArray();
        long length = 0;
        for (int i = 0; i < chars.length; i++) {
            if (!isChineseChar(chars[i])) {
                length += 2;
            } else {
                length++;
            }
        }
        return length;
    }

    public static boolean bettwenLength(String content, int minLength,
                                        int maxLength) {
        return minLength(content, minLength) && maxLength(content, maxLength);
    }

    public static boolean minLength(String content, int minLength) {
        if (StringUtils.isEmpty(content))
            return false;
        return content.length() >= minLength;
    }

    public static boolean maxLength(String content, int maxLength) {
        if (StringUtils.isEmpty(content))
            return false;
        return content.length() <= maxLength;
    }

    public static boolean isContainsAlphanumeric(String content) {
        String regexStr = "^(?!(?:\\d*$))(?!(?:[A-Za-z]*$))[A-Za-z0-9]+$";
        if (StringUtils.isEmpty(content))
            return false;
        Pattern regex = Pattern.compile(regexStr);
        Matcher matcher = regex.matcher(content);
        return matcher.matches();
    }

}
